/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef TestResultAnalysisDialog_H
#define TestResultAnalysisDialog_H

#include <QtGui/QWidget>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QStandardItem>
#include <QDate>
#include <QList>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QMessageBox>
#include <QModelIndexList>
#include <QShortcut>
#include <QDialog>

#include <iostream>
#include <vector>
using namespace std;

#include "ui_TestResultAnalysisDialog.h"

#include "Options.h"
#include "WordManager.h"
#include "MessageBox.h"
#include "SQLiteDB.h"
#include "TestResultAnalysisPlot.h"
#include "ForgettingCurvePlot.h"

class ScanRange
{
public:
	QString name;

	int redBibleBeginList;
	int redBibleEndList;

	ScanRange(QString name);
};

class SelectPolicy
{
public:
	QString name;

	int difficultyLevelMax;
	int difficultyLevelMin;
	int activeFinishedWordCountMax;
	int activeTestingWordsCountMax;
	int activeUntouchedWordsCountMax;

	SelectPolicy(QString name);
};

class TestResultAnalysisDialog : public QDialog
{
    Q_OBJECT

public:
    TestResultAnalysisDialog(QDialog *parent = 0);
    ~TestResultAnalysisDialog();

    int exec();
    virtual void done(int taskType);

private:
    void setScanResultView();
    void createDefaultSelectPolicyFile();
	void createDefaultScanRangeFile();
	void loadSelectPolicies();
	void loadScanRange();
	void fitForgettingCurve();

public slots:
    void scan();
    void showGraph();
    void showForgettingCurve();

	void selectPolicyChanged(int);
	void scanRangeChanged(int);

	void selectWordsToTest();
	void startTest();
	void browse();

	void selectRowAsCandidates();
	void removeRowAsCandidates();
	void clearAllRowCandidates();

private:
    Ui::TestResultAnalysisDialogClass ui;

    TestResultAnalysisPlot* mTestResultAnalysisPlot;
    ForgettingCurvePlot* mForgettingCurvePlot;

    QStandardItemModel* mWordDateModel;
    QStandardItemModel* mCandidatesModel;

    vector<int> mTestingWordIDs;
	vector<int> mNewWordIDs;
	vector<int> mFinishedWordIDs;
	map<int, map<int, vector<int> > > mFinishedWordIDMap; //mFinishedWordIDMap[interval][testCount] = ID vector
	map<int, map<int, TestResultCounter> > mTestResultCounterMap; //mTestResultCounterMap[testCount][interval] = TestResultCounter
	map<int , int> mTestBestIntv; //mTestBestIntv[testCount] = best day interval

	int mCurSelectPolicy;
	vector<SelectPolicy> mSelectPolicies;
	vector<ScanRange> mScanRanges;
	vector<int> mFinalIDs;
};

#endif // TestResultAnalysisDialog_H
